package com.projectsexception.festivales.util;

import java.util.Comparator;

import com.projectsexception.festivales.setup.ShowInfo;

public class ShowInfoComparator implements Comparator<ShowInfo> {
    
    private static final int LAST_SHOW = 8;
    private static final int HOURS_IN_DAY = 24;

    @Override
    public int compare(ShowInfo showInfo1, ShowInfo showInfo2) {
        int compare = showInfo1.getDate().compareTo(showInfo2.getDate());
        if (compare == 0) {
            String[] time1 = showInfo1.getStartTime().split(":");
            String[] time2 = showInfo2.getStartTime().split(":");
            if (time1.length == 2 && time2.length == 2) {
                int hour1 = Integer.parseInt(time1[0]);
                if (hour1 < LAST_SHOW) {
                    hour1 += HOURS_IN_DAY;
                }
                int hour2 = Integer.parseInt(time2[0]);                
                if (hour2 < LAST_SHOW) {
                    hour2 += HOURS_IN_DAY;
                }
                compare = hour1 - hour2;
                if (compare == 0) {
                    compare = Integer.parseInt(time1[1]) - Integer.parseInt(time2[1]);
                }
            }
        }
        return compare;
    }

}
